package com.gfxy.modules.items.service;

import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.aspect.annotation.Dict;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

/**
 * @author: lijixin 2021/9/27 12:48 下午
 */
@Service
public class DictTranslater {

    @Autowired
    private CommonAPI commonAPI;

    public String getDictValue(Class clazz, String fieldName,String value) throws NoSuchFieldException {

        Field field = clazz.getDeclaredField(fieldName);
        String code = ((Dict)field.getAnnotation(Dict.class)).dicCode();
        String text = ((Dict)field.getAnnotation(Dict.class)).dicText();
        String table = ((Dict)field.getAnnotation(Dict.class)).dictTable();
        String key = String.valueOf(value);
        String textValue = this.translateDictValue(code, text, table, key);
        return textValue;
    }

    private  String translateDictValue(String code, String text, String table, String key) {
        if (oConvertUtils.isEmpty(key)) {
            return null;
        } else {
            StringBuffer textValue = new StringBuffer();
            String[] keys = key.split(",");
            String[] var7 = keys;
            int var8 = keys.length;

            for(int var9 = 0; var9 < var8; ++var9) {
                String k = var7[var9];
                String tmpValue = null;

                if (k.trim().length() != 0) {
                    if (!StringUtils.isEmpty(table)) {
                        tmpValue = this.commonAPI.translateDictFromTable(table, text, code, k.trim());
                    } else {
                        tmpValue = this.commonAPI.translateDict(code, k.trim());
                    }

                    if (tmpValue != null) {
                        if (!"".equals(textValue.toString())) {
                            textValue.append(",");
                        }

                        textValue.append(tmpValue);
                    }
                }
            }

            return textValue.toString();
        }
    }
}
